package org.yohann.ai.provider;

import org.yohann.ai.pojo.BillingInfo;
import org.yohann.ai.pojo.ServiceInfo;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

public class BillingInfoProvider {
    public static List<BillingInfo> getBillingInfo() {
        Random random = new Random();
        List<ServiceInfo> allServiceInfo = ServiceInfoProvider.getAllServiceInfo();

        Map<String, List<ServiceInfo>> serviceMap = allServiceInfo.stream()
                .filter(service -> Arrays.asList("monthly", "yearly").contains(service.getPlanType()))
                .collect(Collectors.groupingBy(ServiceInfo::getServiceType));

        List<ServiceInfo> baseService = new ArrayList<>();
        for (List<ServiceInfo> value : serviceMap.values()) {
            if (random.nextDouble(1) < 0.6) {
                ServiceInfo selected = value.get(random.nextInt(value.size()));
                baseService.add(selected);
            }
        }

        List<BillingInfo> list = new ArrayList<>();
        for (int i = 0; i < random.nextInt(3, 12); i++) {
            ArrayList<ServiceInfo> serviceInfoList = new ArrayList<>(baseService);
            if (random.nextDouble(1) < 0.4) {
                serviceInfoList.remove(random.nextInt(serviceInfoList.size()));
            }
            if (random.nextDouble(1) < 0.6) {
                serviceInfoList.add(allServiceInfo.get(random.nextInt(allServiceInfo.size())));
            }

            String amount = serviceInfoList.stream().map(ServiceInfo::getAmount).map(BigDecimal::new).reduce(BigDecimal.ZERO, BigDecimal::add).toString();
            BillingInfo billingInfo = new BillingInfo();
            billingInfo.setMonth(12 - i + "月账单");
            billingInfo.setAmount(amount);
            billingInfo.setServiceInfoList(serviceInfoList);

            list.add(billingInfo);
        }

        return list;
    }
}
